import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { Router } from '@angular/router';
import { MyModalHelper } from '../../../core/my-modal.helper';
import { MarketBandService } from '../../../core/routes-services/market-band.service';
import { YESNO_OPTION } from '../../../constant/dictionary';

@Component({
  selector: 'app-market-band-list',
  templateUrl: './market-band-list.component.html',
})
export class MarketBandListComponent implements OnInit {

  isLoading = false;
  keyword: any;
  status: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  yesOrNo:any [] = YESNO_OPTION;
  constructor(private message: NzMessageService,
              private router: Router,
              private marketBandService: MarketBandService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }


  initPage() {
    this.isLoading = true;
    this.marketBandService.pagination(this.page, this.keyword, this.status).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  addEntity = () => {
    this.router.navigate(['/band/list/edit'], { queryParams: { id: 0 } });
  };

  auditing = (data) => {
    this.marketBandService.auditingEntity(data.id).then((data) => {
      this.search();
      this.message.success('操作成功');
    }, error => console.log('操作出错，后续处理'));
  };

}
